<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <title>修改用户</title>
</head>
<body>

<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav"
            aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNav">
        <ul class="navbar-nav">
            <li class="nav-item active">
                <a class="nav-link" href="/admin/user_index.html">用户管理</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="/admin/pet_index.html">宠物管理</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="/admin/adopt_index.html">领养管理</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="/logout">退出</a>
            </li>
        </ul>
    </div>
</nav>

<div class="container mt-4">
    <div class="row">
        <div class="col-md-12">
            <h2>修改用户</h2>
            <form action="/admin/user/update" method="post">
                <input type="hidden" id="id" name="id">
                <div class="form-group">
                    <label for="username">用户名称:</label>
                    <input type="text" class="form-control" id="username" name="username" placeholder="请输入用户名">
                </div>
                <div class="form-group">
                    <label for="password">用户密码:</label>
                    <input type="password" class="form-control" id="password" name="password"
                           placeholder="请输入用户密码">
                </div>
                <div class="form-group">
                    <label for="tel">联系方式:</label>
                    <input type="text" class="form-control" id="tel" name="tel" placeholder="请输入联系方式">
                </div>
                <div class="form-group">
                    <label for="gender">用户性别:</label>
                    <select id="gender" name="gender" class="form-control">
                        <option value="男">男</option>
                        <option value="女">女</option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="address">联系地址:</label>
                    <input type="text" class="form-control" id="address" name="address" placeholder="请输入联系地址">
                </div>
                <button type="submit" class="btn btn-primary">保存</button>
            </form>
        </div>
    </div>
</div>

<script src="/js/jquery.min.js"></script>
<script>
    $(document).ready(function () {
        var userId = getParameterByName("id");
        $.ajax({
            url: "/admin/user/get?id=" + userId,
            type: "GET",
            dataType: "json",
            success: function (data) {
                $("#id").val(data.id);
                $("#username").val(data.username);
                $("#password").val(data.password);
                $("#tel").val(data.tel);
                $("#gender").val(data.gender);
                $("#address").val(data.address);
            },
            error: function () {
                console.error("加载失败");
            }
        });
    });

    function getParameterByName(name, url) {
        if (!url) url = window.location.href;
        name = name.replace(/[\[\]]/g, "\\$&");
        var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
            results = regex.exec(url);
        if (!results) return null;
        if (!results[2]) return '';
        return decodeURIComponent(results[2].replace(/\+/g, " "));
    }
</script>

</body>
</html>
